package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.DicType;
import com.jframe.cfg.model.DataSrc;
import com.jframe.cfg.model.Iframe;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.model.Tablemapping;
import com.jframe.cfg.service.DictypeService;
import com.jframe.cfg.service.IframeService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.cfg.service.TablemapService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class DataSrcAction extends ExtendAction {
	
	private static String ACTION="dataSrcAction";
	private static String SERVICE="dataSrcService";
	private static Class<DataSrc> CLAZZ=DataSrc.class;
	
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();//活动列
		frozenColumns.add(new Columns("ck","",40,""));
		commonColumns.add(new Columns("id", "编号", 100,""));
		commonColumns.add(new Columns("fieldname", "权限字段", 100,""));
		commonColumns.add(new Columns("name", "权限字段名称", 120,""));
		commonColumns.add(new Columns("datasrc","权限数据源",120,
				new Editor("combobox",true),this.dictype()));
		commonColumns.add(new Columns("iframeId","页签名称",120,
				new Editor("combobox"),this.getMenuIframe(this.pageInfo.getPageTabId(request))));
		commonColumns.add(new Columns("type","条件类型",120,
				new Editor("combobox"),DicCache.getComboboxJson("dm_sys_searchtype")));
		commonColumns.add(new Columns("isValidate","是否需要权限验证",120,
				new Editor("combobox"),DicCache.getComboboxJson("dm_sys_isvalidate")));
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));		
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid );
		this.request.setAttribute("js", js);
		this.forward("/jframe/cfg/list/dataSrcList.jsp");
	}
	
	public void load(){
		this.baseLoad(SERVICE,CLAZZ);
	}
	
	public Object beforeSave(Object object){
		DataSrc dataSrc = (DataSrc) object;
		//if(dataSrc.getId()==0){
			IframeService iframeService = (IframeService)SpringUtil.getBean("iframeService");
			Iframe iframe = iframeService.load(StringUtil.parseInt(dataSrc.getIframeId()));
			dataSrc.setTabId(iframe.getTabId());
		//}
		return dataSrc;
	}
	public void save(){
		this.baseSave(SERVICE, CLAZZ);
	}
	
	public void afterSave(Object object){		
		DataSrc dataSrc = (DataSrc) object;
		//保留当前tab页中的索引树
		if(dataSrc.getType()==1){
			IframeService iframeService = (IframeService)SpringUtil.getBean("iframeService");
			Iframe iframe = iframeService.load(StringUtil.parseInt(dataSrc.getIframeId()));
			iframe.setDatasrcId(dataSrc.getId());
			iframeService.save(iframe);
		}
	}
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void getObjById(){		 
		this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	} 
	
	private String getIframeCombobox(String tabId){
		//IframeService iframeService = (IframeService)this.getBean("iframeService");
		//获取根节点下的子节点，然后根据子节点查询出所有的
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> list = sysmenuService.findListByUpIdAndGroupId(1,StringUtil.parseInt(tabId));
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		for(Sysmenu menu:list){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", "root"+menu.getId());
			map.put("text", menu.getText());
			map.put("children", this.getChildrens(menu.getId()));
			maps.add(map);
		}
		return JsonUtil.list2Json(maps);
	}
	
	private List<Map<String,Object>> getChildrens(int upId){
		IframeService iframeService = (IframeService)this.getBean("iframeService");		
		return iframeService.findListMapByMenuUpId(upId);
	}
	
	private String getMenuIframe(String tabId){
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		int iframeId = StringUtil.parseInt(this.pageInfo.getPageIframeId(request));
		//if(iframeId>0){
		///	Iframe iframe = iframeService.load(iframeId);
		//	List<Map<String,Object>> listmap = iframeService.findListMap(iframe.getGroupId());	
		//	return JsonUtil.list2Json(listmap);
		//}else{
			List<Map<String,Object>> listmap = iframeService.findListMap(StringUtil.parseInt(tabId));	
			return JsonUtil.list2Json(listmap);
		//}
	}
	
	public void getIframes(){
		this.print(getIframeCombobox(this.pageInfo.getPageTabId(this.request)));
	}
	
	private String dictype(){
		String json="[";
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");
		TablemapService tablemapService = (TablemapService)this.getBean("tablemapService");
		List<DicType> diclist= dictypeService.findAll();
		List<Tablemapping> maplist = tablemapService.findAll();
		for(DicType dictype:diclist){
			json +="{\"id\":\""+dictype.getEnname()+"\",\"text\":\""+dictype.getCnname()+"\"},";
		}
		for(Tablemapping maptype:maplist){
			json +="{\"id\":\""+maptype.getEnname()+"\",\"text\":\""+maptype.getCnname()+"\"},";
		}
		if(json.length()>3){
			json = json.substring(0,json.length()-1);
		}
		json +="]";
		return json;
	}

}
